package com.buaa.utility.servlet;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.buaa.utility.Constants;


public class AuthFilter implements Filter{
	
//	private Map<String,Object> publicPages;
	private String loginPage;

	public void init(FilterConfig config) throws ServletException {
		
		loginPage = config.getInitParameter("loginPage");
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filters) throws IOException, ServletException {
		String uri = ((HttpServletRequest) request).getRequestURI().toLowerCase();
		if(uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".jpg") || uri.endsWith(".gif")){
			filters.doFilter(request, response);
		}else{
			Object obj = ((HttpServletRequest) request).getSession().getAttribute(Constants.SESSION_KEY_USER);
		    if (obj == null)
		      ((HttpServletRequest) request).getRequestDispatcher(this.loginPage).forward(request, response);
		    else
		      filters.doFilter(request, response);
		}
	}

	public void destroy() {
	}

}
